package com.lxj.fast_android_demo.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.lxj.fast_android_demo.dto.Constant;
import com.lxj.fast_android_demo.dto.DataRecord;
import com.lxj.fast_android_demo.dto.Dataset;
import com.lxj.fast_android_demo.entity.TableHeadColumn;
import com.lxj.fast_android_demo.view.CustomTable;
import com.lxj.fast_android_demo.view.MeapTable;
import com.lxj.fast_android_demo.view.UIMeapTable;

public class MainAc extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(prepareTableData());
		setContentView(prepareUITableData());
		// checkDataset();
	}

	private Dataset checkDataset() {
		// 列定义数据
		List<HashMap<String, String>> fieldDatas = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> fieldData = new HashMap<String, String>();
		fieldData.put(Constant.FIELD_NAME, "empName");
		fieldData.put(Constant.FIELD_TYPE, "1");
		fieldData.put(Constant.FIELD_DEFAULT, "梁香菊");
		fieldDatas.add(fieldData);
		fieldData = new HashMap<String, String>();
		fieldData.put(Constant.FIELD_NAME, "empGener");
		fieldData.put(Constant.FIELD_TYPE, "1");
		fieldData.put(Constant.FIELD_DEFAULT, "0");
		fieldDatas.add(fieldData);
		fieldData = new HashMap<String, String>();
		fieldData.put(Constant.FIELD_NAME, "empAge");
		fieldData.put(Constant.FIELD_TYPE, "2");
		fieldData.put(Constant.FIELD_DEFAULT, "20.5");
		fieldDatas.add(fieldData);
		fieldData = new HashMap<String, String>();
		fieldData.put(Constant.FIELD_NAME, "memo");
		fieldData.put(Constant.FIELD_TYPE, "1");
		fieldData.put(Constant.FIELD_DEFAULT, "这个人很懒什么也没留下");
		fieldDatas.add(fieldData);
		// fieldData = new HashMap<String, String>();
		// fieldData.put(Constant.FIELD_NAME, "result");
		// fieldData.put(Constant.FIELD_TYPE, "3");
		// fieldData.put(Constant.FIELD_DEFAULT, "0.0");
		// fieldDatas.add(fieldData);

		Dataset dataset = new Dataset(fieldDatas);

		dataset.addRecord();
		HashMap<String, String> recordDatas = new HashMap<String, String>();
		recordDatas.put("empName", "梁香香");
		recordDatas.put("empGener", "1");
		recordDatas.put("empAge", "26");
		recordDatas.put("memo", "哈哈\n移动研发部门的老大，俗称大陈杰，很牛叉呀");
		dataset.addRecord(recordDatas);
		recordDatas = new HashMap<String, String>();
		recordDatas.put("empName", "颜舒波");
		recordDatas.put("empGener", "0");
		recordDatas.put("empAge", "30");
		recordDatas.put("memo1", "memo1memo1");
		dataset.addRecord(recordDatas);
		recordDatas = new HashMap<String, String>();
		recordDatas.put("empName", "王爽");
		recordDatas.put("empGener", "1");
		recordDatas.put("empAge", "");
		recordDatas.put("memo", "移动研发部门的开发程序猿，是女程序猿噢，现在女程序猿也很多呀呀呀呀呀");
		dataset.addRecord(recordDatas);

		// 总记录遍历
		dataset.goBeforeFirst();
		while (dataset.goNext()) {
			DataRecord record = dataset.getRecord();
			String logContent = "";
			for (String key : record.getDatas().keySet()) {
				logContent += key + ":" + String.valueOf(record.getData(key))
						+ "_";
			}
			Log.d(this.getClass().getName(), logContent);
		}

		// 移至第二条记录并将姓名修改为梁香菊
		dataset.goToIndex_(1);
		DataRecord record = dataset.getRecord();
		record.setValueForField("陈杰", "empName");
		String logContent = "";
		for (String key : record.getDatas().keySet()) {
			logContent += key + ":" + record.getValue(key) + "_";
		}
		Log.d(this.getClass().getName(), "dataSet移至第二条记录并修改名字后record数据为:"
				+ logContent);

		// 新增一条初始记录
		dataset.addRecord();
		DataRecord recordNew = dataset.getRecord();
		String logContentNew = "";
		for (String key : recordNew.getDatas().keySet()) {
			logContentNew += key + ":" + recordNew.getValue(key) + "_";
		}
		Log.d(this.getClass().getName(), "dataSet新增一条初始记录为:" + logContentNew);

		// 删除当前记录（刚新增的记录）
		if (dataset.deleteRecord()) {
			dataset.goBeforeFirst();
			while (dataset.goNext()) {
				DataRecord eachRecord = dataset.getRecord();
				String log = "";
				for (String key : eachRecord.getDatas().keySet()) {
					log += key + ":" + String.valueOf(eachRecord.getData(key))
							+ "_";
				}
				Log.d(this.getClass().getName(), "新增的记录被删除后总记录为:" + log);
			}
		}

		return dataset;
	}

	private CustomTable prepareTableData() {
		// table表头数据
		List<TableHeadColumn> headList = new ArrayList<TableHeadColumn>();
		TableHeadColumn headColumn = new TableHeadColumn(1, "empName", "姓名");
		headList.add(headColumn);
		headColumn = new TableHeadColumn(2, "empAge", "年龄");
		headList.add(headColumn);
		headColumn = new TableHeadColumn(3, "empGender", "性别");
		headList.add(headColumn);
		headColumn = new TableHeadColumn(4, "memo", "备注");
		headList.add(headColumn);
		headColumn = new TableHeadColumn(5, "factor1", "factor1");
		headList.add(headColumn);
		headColumn = new TableHeadColumn(6, "factor2", "factor2");
		headList.add(headColumn);
		headColumn = new TableHeadColumn(7, "result", "结果");
		headList.add(headColumn);
		// table数据集
		List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("empName", "梁香菊");
		map.put("empAge", 30);
		map.put("empGender", 0);
		map.put("memo", "aaa");
		map.put("factor1", 1);
		map.put("factor2", 11);
		map.put("result", 0.0);
		dataList.add(map);
		map = new HashMap<String, Object>();
		map.put("empName", "付海峰");
		map.put("empAge", 22);
		map.put("empGender", 1);
		map.put("memo", "bbb");
		map.put("factor1", 2);
		map.put("factor2", 22);
		map.put("result", 0.0);
		dataList.add(map);
		map = new HashMap<String, Object>();
		map.put("empName", "颜舒波");
		map.put("empAge", 28);
		map.put("empGender", 1);
		map.put("memo", "ccc");
		map.put("factor1", 3);
		map.put("factor2", 33);
		map.put("result", 0.0);
		dataList.add(map);
		map = new HashMap<String, Object>();
		map.put("empName", "王爽");
		map.put("empAge", 23);
		map.put("empGender", 0);
		map.put("memo", "ddd");
		map.put("factor1", 4);
		map.put("factor2", 44);
		map.put("result", 0.0);
		dataList.add(map);
		map = new HashMap<String, Object>();
		map.put("empName", "韩晶");
		map.put("empAge", 31);
		map.put("empGender", 1);
		map.put("memo", "eee");
		map.put("factor1", 5);
		map.put("factor2", 55);
		map.put("result", 0.0);
		dataList.add(map);
		map = new HashMap<String, Object>();
		map.put("empName", "申莎莎");
		map.put("empAge", 24);
		map.put("empGender", 0);
		map.put("memo", "fff");
		map.put("factor1", 6);
		map.put("factor2", 66);
		map.put("result", 0.0);
		dataList.add(map);
		map = new HashMap<String, Object>();
		map.put("empName", "陈杰");
		map.put("empAge", 25);
		map.put("empGender", 0);
		map.put("memo", "这个人很懒，没有留memo");
		map.put("factor1", 0.0);
		map.put("factor2", 0.0);
		map.put("result", 0.0);
		dataList.add(map);
		return new CustomTable(this, headList, dataList);
	}

	private MeapTable prepareMeapTableData() {
		// 列定义数据
		List<HashMap<String, String>> fieldDatas = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> fieldData = new HashMap<String, String>();
		fieldData.put(Constant.FIELD_NAME, "empName");
		fieldData.put(Constant.FIELD_TYPE, "1");
		fieldData.put(Constant.FIELD_DEFAULT, "陈杰");
		fieldDatas.add(fieldData);
		fieldData = new HashMap<String, String>();
		fieldData.put(Constant.FIELD_NAME, "empAge");
		fieldData.put(Constant.FIELD_TYPE, "2");
		fieldData.put(Constant.FIELD_DEFAULT, "20.5");
		fieldDatas.add(fieldData);
		fieldData = new HashMap<String, String>();
		fieldData.put(Constant.FIELD_NAME, "empGender");
		fieldData.put(Constant.FIELD_TYPE, "2");
		fieldData.put(Constant.FIELD_DEFAULT, "0");
		fieldDatas.add(fieldData);
		fieldData = new HashMap<String, String>();
		fieldData.put(Constant.FIELD_NAME, "memo");
		fieldData.put(Constant.FIELD_TYPE, "1");
		fieldData.put(Constant.FIELD_DEFAULT, "这个人很懒什么也没留下");
		fieldDatas.add(fieldData);
		fieldData = new HashMap<String, String>();
		fieldData.put(Constant.FIELD_NAME, "factor1");
		fieldData.put(Constant.FIELD_TYPE, "3");
		fieldData.put(Constant.FIELD_DEFAULT, "0.0");
		fieldDatas.add(fieldData);
		fieldData = new HashMap<String, String>();
		fieldData.put(Constant.FIELD_NAME, "factor2");
		fieldData.put(Constant.FIELD_TYPE, "3");
		fieldData.put(Constant.FIELD_DEFAULT, "0.0");
		fieldDatas.add(fieldData);
		fieldData = new HashMap<String, String>();
		fieldData.put(Constant.FIELD_NAME, "result");
		fieldData.put(Constant.FIELD_TYPE, "3");
		fieldData.put(Constant.FIELD_DEFAULT, "0.0");
		fieldDatas.add(fieldData);

		Dataset dataset = new Dataset(fieldDatas);

		dataset.addRecord();
		dataset.addRecord();
		dataset.addRecord();
		HashMap<String, String> recordDatas = new HashMap<String, String>();
		recordDatas.put("empName", "梁香香");
		recordDatas.put("empAge", "26");
		recordDatas.put("memo", "哈哈");
		recordDatas.put("result", "3.34");
		dataset.addRecord(recordDatas);
		recordDatas = new HashMap<String, String>();
		recordDatas.put("empName", "王爽");
		recordDatas.put("empAge", "");
		recordDatas.put("memo1", "memo1memo1");
		recordDatas.put("result", "");
		dataset.addRecord(recordDatas);
		return new MeapTable(this, dataset);
	}

	private UIMeapTable prepareUITableData() {
		String tableJson = "{\"fields\":{\"fieldA\":{\"label\":\"\",\"dataIndex\":\"empName\"},\"fieldB\":{\"label\":\"\",\"dataIndex\":\"empGener\"},\"fieldC\":{\"label\":\"\",\"dataIndex\":\"empAge\"},\"fieldD\":{\"label\":\"\",\"dataIndex\":\"memo\"}},\"layout\":{\"type\":\"Liner\",\"orientation\":0,\"style\":{\"origin\":{\"left\":0,\"top\":0},\"size\":{\"width\":\"-1\",\"height\":\"parseTableRowHeight\"},\"padding\":{\"left\":10,\"top\":10,\"right\":10,\"bottom\":10},\"margin\":{\"left\":0,\"top\":0,\"right\":0,\"bottom\":0},\"font\":{\"weight\":\"bold\",\"color\":\"#ff00ff\"}},\"contents\":[{\"fieldId\":\"fieldA\",\"style\":{\"size\":{\"width\":\"200\",\"height\":\"-1\"},\"font\":{\"size\":16,\"family\":\"黑体\",\"weight\":\"italic\",\"color\":\"parseNameColor\"}}},{\"type\":\"Liner\",\"orientation\":-1,\"style\":{\"size\":{\"width\":\"-1\",\"height\":\"-2\"}},\"contents\":[{\"type\":\"Liner\",\"orientation\":0,\"style\":{\"size\":{\"width\":\"-1\",\"height\":\"100\"}},\"contents\":[{\"fieldId\":\"fieldB\",\"style\":{\"size\":{\"width\":\"300\",\"height\":\"-1\"}}},{\"fieldId\":\"fieldC\",\"style\":{\"size\":{\"width\":\"-1\",\"height\":\"-1\"}}}]},{\"fieldId\":\"fieldD\",\"style\":{\"size\":{\"width\":\"-1\",\"height\":\"-2\"},\"font\":{\"size\":12,\"family\":\"黑体\",\"weight\":\"bold\",\"color\":\"#7733ee\"}}}]}]}}";
		return new UIMeapTable(this, checkDataset(), tableJson);
		
		/*UITable table = UITableUtil.parseTable(tableJson);
		if(null != table){
			table.setDataset(checkDataset());
		}
		return  new UIJMeapTable(this,table);*/
	}

	/*private void prepareUITable() {
		HashMap<String, ControlField> fields = new HashMap<String, ControlField>();
		fields.put("fieldA", new ControlField("", "empName"));
		fields.put("fieldB", new ControlField("", "empAge"));
		fields.put("fieldC", new ControlField("", "memo"));
		fields.put("fieldD", new ControlField("", "result"));

		List<UIView> layout1Contents = new ArrayList<UIView>();
		layout1Contents.add(new UIControl("fieldB", null));
		layout1Contents.add(new UIControl("fieldC", null));

		UIStyle style = new UIStyle(new UIOrigin(0, 0), new UISize(300, 200),
				new UIPadding(0, 0, 0, 0), new UIMargin(0, 0, 0, 0));
		UILayout layoutBC = new UILayout(UILayoutType.LINER,
				UIOrientationType.Vertical, layout1Contents, style);

		List<UIView> contents = new ArrayList<UIView>();
		contents.add(new UIControl("fieldA", null));
		contents.add(layoutBC);
		contents.add(new UIControl("fieldD", null));
		UILayout layout = new UILayout(UILayoutType.LINER,
				UIOrientationType.Horizon, contents, new UIStyle(null,
						new UISize(1024, 200), null, null));
		UITable table = new UITable(checkDataset(), fields, layout);
	}*/
}
